package com.xxxx.web;

import com.xxxx.domain.Course;
import com.xxxx.service.CourseService;
import com.xxxx.service.impl.CourseServiceImpl;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Show {
    static CourseService courseService = new CourseServiceImpl();
    public static void main(String[] args) throws IOException {
        //键盘录入
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入你要选择的功能：1.导入 2.导出");
        int num=scanner.nextInt();
        if(num==1){
            //导入
            //读取excel表中的数据
            System.out.println("请输入要导入的文件地址（不包含空格）");
            String path= scanner.next();
            List<Course> courseList=read(path);
            System.out.println(courseList);
            //将数据写入数据库
            courseService.save(courseList);
        }else if(num==2){
            //导出
            //读取数据库
            //将数据写到excel表格中
            List<Course> courseList=courseService.findAll();
            System.out.println(courseList);
            System.out.println("写出文件位置");
            String path=scanner.next();
            write(courseList,path);
        }else {
            System.out.println("输入有误，请重新启动");
        }

    }
    public static List<Course> read(String path) throws IOException {
        List<Course> courseList=new ArrayList<>();
        XSSFWorkbook workbook = new XSSFWorkbook(path);
        XSSFSheet sheet=workbook.getSheetAt(0);
        int lastRowNum=sheet.getLastRowNum();
        for (int i = 0; i <=lastRowNum ; i++) {
            XSSFRow row=sheet.getRow(i);
            if(row!=null)
            {
                List<String> list=new ArrayList<>();
                for (Cell cell:row) {
                    if (cell !=null){
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        String value= cell.getStringCellValue();
                        if(!"".equals(value)){
                            list.add(value);
                        }
                    }
                }
                if(list.size()>0) {
                    Course course = new Course(Integer.parseInt(list.get(0)), list.get(1),Integer.parseInt(list.get(2)));
                    courseList.add(course);
                }
            }
        }
        return courseList;
    }
    public static void write(List<Course> courseList,String path) throws IOException {
        XSSFWorkbook xssfWorkbook=new XSSFWorkbook();//创建工作簿
        XSSFSheet sheet = xssfWorkbook.createSheet("课程");
        //创建单元格样式
        XSSFCellStyle cellStyle=xssfWorkbook.createCellStyle();
        cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());//背景色
        cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        //字体样式
        XSSFFont font=xssfWorkbook.createFont();
        font.setFontName("黑体");//字体
        font.setColor(IndexedColors.GREEN.getIndex());//字体颜色
        cellStyle.setFont(font);

        XSSFRow row = sheet.createRow(0);

        /*row.createCell(0).setCellValue("课程号");
        row.createCell(1).setCellValue("课程名");
        row.createCell(2).setCellValue("课时");*/
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("课程号");
        cell.setCellStyle(cellStyle);

        XSSFCell cell1 = row.createCell(1);
        cell1.setCellValue("课程名");
        cell1.setCellStyle(cellStyle);

        XSSFCell cell2 = row.createCell(2);
        cell2.setCellValue("课时");
        cell2.setCellStyle(cellStyle);

        for (int i = 0; i < courseList.size(); i++) {
            XSSFRow row1=sheet.createRow(i+1);
            row1.createCell(0).setCellValue(courseList.get(i).getCno());
            row1.createCell(1).setCellValue(courseList.get(i).getCname());
            row1.createCell(2).setCellValue(courseList.get(i).getClassHour());
        }
        FileOutputStream out=new FileOutputStream(path);
        xssfWorkbook.write(out);
        out.flush();
        out.close();
        xssfWorkbook.close();
    }
}
